package com.api.sw.duban.service;

import com.api.sw.duban.annotation.Table;
import com.api.sw.duban.util.BaseUtil;
import com.api.sw.duban.util.LogTool;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.*;

public class InsertHrBaseService<T> implements InsertHr{
    private final static LogTool log = new LogTool("/log/assetlog/huangwm/InsertHrBaseService",false);
    private Class clazz=null;
    public InsertHrBaseService(){
            ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
            clazz = (Class<T>) pt.getActualTypeArguments()[0] ;
            System.out.println(clazz.getName());
    }
    @Override
    public Map<String, Object> PaseModeByAnno(Object obj) throws Exception {
        //设置对象  设置
        Map<String, Object> map=new HashMap<String, Object>();
        String tablename="";
        String datasouce="";
        String sqlbuffer="";//存储sql 语句  datasoruce+sql
        Object[] objval=new Object[0];//存储值
        if(obj!=null&&clazz!=null){
            Table Annotations=(Table)clazz.getAnnotation(Table.class);
            datasouce=Annotations.datasource();
            tablename=Annotations.value();
            map.put("datasource",datasouce);
            Field[] Fields= clazz.getDeclaredFields();
            for(Field Field1:Fields){
                Field1.setAccessible(true);//设置可读属性
                String fieldname=Field1.getName();
                Object val=Field1.get(obj);
                com.api.sw.duban.annotation.Field FieldAn=( com.api.sw.duban.annotation.Field)Field1.getAnnotation( com.api.sw.duban.annotation.Field.class);
                //Annotation[] FieldAnnotations=Field.getAnnotations();
                if(FieldAn!=null){
                    String dbFiledName=FieldAn.dbFiledName();//数据库字段名称
                    String Pmkey=FieldAn.Pmkey();//主键
                    String CoverMothed=FieldAn.CoverMothed();//转换方法
                    String value=FieldAn.value();//值
                    //主键暂时不写入
                    if("1".equals(Pmkey)){
                        continue;
                    }
                    if(dbFiledName==null||"".equals(dbFiledName)){
                        dbFiledName=fieldname;
                    }
                    if(value!=null&&!"".equals(value)){
                        val=value;
                    }
                    sqlbuffer=sqlbuffer+dbFiledName+",";
                    //设置值
                    objval = Arrays.copyOf(objval, (objval.length+1));
                    objval[objval.length-1] = val;


                }else{
                    continue;
                }
            }
            if(objval.length>0&&!"".equals(sqlbuffer)){
                if(sqlbuffer.endsWith(",")){
                    sqlbuffer=sqlbuffer.substring(0,sqlbuffer.length()-1);
                }
                String wh= BaseUtil.getWh(objval.length);
                log.writeLog(wh);
                map.put("sql","insert into "+tablename+"("+sqlbuffer+")values("+wh+")");
                map.put("params",objval);
            }
        }
        return map;
    }

    @Override
    public List<Map<String, Object>> PaseModeByAnnoList(List<Object> list,List<Map<String, Object>> listMap) throws Exception {
        if(listMap==null){
            listMap=new ArrayList<Map<String, Object>>();
        }
        //List<Map<String, Object>> listMap=new ArrayList<Map<String, Object>>();
        if(list==null||list.size()<=0){
            return listMap;
        }
        for(Object obj:list) {
            Map<String, Object> map = new HashMap<String, Object>();
            String tablename = "";
            String datasouce = "";
            String sqlbuffer = "";//存储sql 语句  datasoruce+sql
            Object[] objval = new Object[0];//存储值
            if (obj != null && clazz != null) {
                Table Annotations = (Table) clazz.getAnnotation(Table.class);
                datasouce = Annotations.datasource();
                tablename = Annotations.value();
                map.put("datasource", datasouce);
                Field[] Fields = clazz.getDeclaredFields();
                for (Field Field1 : Fields) {
                    Field1.setAccessible(true);//设置可读属性
                    String fieldname = Field1.getName();
                    Object val = Field1.get(obj);
                    com.api.sw.duban.annotation.Field FieldAn = ( com.api.sw.duban.annotation.Field) Field1.getAnnotation( com.api.sw.duban.annotation.Field.class);
                    //Annotation[] FieldAnnotations=Field.getAnnotations();
                    if (FieldAn != null) {
                        String dbFiledName = FieldAn.dbFiledName();//数据库字段名称
                        String Pmkey = FieldAn.Pmkey();//主键
                        String CoverMothed = FieldAn.CoverMothed();//转换方法
                        String value = FieldAn.value();//值
                        //主键暂时不写入
                        if ("1".equals(Pmkey)) {
                            continue;
                        }
                        if (dbFiledName == null || "".equals(dbFiledName)) {
                            dbFiledName = fieldname;
                        }
                        if (value != null && !"".equals(value)) {
                            val = value;
                        }
                        sqlbuffer = sqlbuffer + dbFiledName + ",";
                        //设置值
                        objval = Arrays.copyOf(objval, (objval.length + 1));
                        objval[objval.length - 1] = val;


                    } else {
                        continue;
                    }
                }
                if (objval.length > 0 && !"".equals(sqlbuffer)) {
                    if (sqlbuffer.endsWith(",")) {
                        sqlbuffer = sqlbuffer.substring(0, sqlbuffer.length() - 1);
                    }
                    String wh = BaseUtil.getWh(objval.length);
                    map.put("sql", "insert into " + tablename + "(" + sqlbuffer + ")values(" + wh + ")");
                    map.put("params", objval);
                    listMap.add(map);
                }
            }
        }
        return listMap;
    }
    public String getObjMap(Object obj){
        return "";
    }
    public String getObjMapList(List<Object> obj,List<Map<String, Object>> listMap){
        return "";
    }
}
